package com.yaodian100.mall.survey.web;

import java.io.IOException;
import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;

import org.apache.commons.lang.StringUtils;

import com.opensymphony.xwork2.ActionSupport;
import com.yaodian100.core.template.service.TemplateService;
import com.yaodian100.ec.member.entity.Member;
import com.yaodian100.ec.member.entity.MemberMemo;
import com.yaodian100.ec.member.entity.MemberProfile;
import com.yaodian100.ec.member.service.MemberService;
import com.yaodian100.ec.survey.entity.Survey;
import com.yaodian100.ec.survey.entity.SurveyData;
import com.yaodian100.ec.survey.service.SurveyService;
import com.yaodian100.mall.member.web.UserLoginData;

/**
 * 
 * @author apple.chang
 * 
 */
public class SurveyAction extends ActionSupport {

	/**
	 * 
	 */
	private static final long serialVersionUID = 2159731593203540201L;

	private SurveyService surveyService;
	private MemberService memberService;
	private TemplateService velocityService;
	private String surveyId;
	private Member member;
	private MemberProfile memberProfile;
	private SurveyData surveyData;
	private String result;

	private String sex;
	private String birth;

	private String married;
	private String edu;
	private String height;
	private String weight;
	private String work;
	private String workType;
	private String shoesSize;
	private String salary;

	private String co1;
	private String co2;
	private String co3;
	private String co4;
	private String co5;
	private String co6;
	private String co7;
	private String co8;
	private String co9;
	private String co10;
	private String co11;
	private String co12;
	private String co13;
	private String co14;
	private String co15;
	private String co16;
	private String co17;
	private String co18;
	private String co19;
	private String co20;
	private String co21;
	private String co22;
	private String co23;
	private String co24;
	private String co25;
	private String co26;
	private String co27;
	private String co28;
	private String co29;
	private String co30;

	private String body;

	public String survey() throws IOException {
		if (StringUtils.isBlank(surveyId)) {
			return "error";
		}
		Survey survey = surveyService.getSurvey(surveyId);
		if (null == survey) {
			return "error";
		}
		if (survey.getStartDate().getTime() > System.currentTimeMillis()
				|| survey.getEndDate().getTime() < System.currentTimeMillis()) {
			return "overdue";
		}
		String memberId = UserLoginData.getMemberId();
		if (memberService.isJoined(memberId, surveyId)) {
			return "isJoined";
		}

		member = memberService.get(memberId);
		memberProfile = memberService.getMemberProfile(memberId);

		Map<String, Object> m = new HashMap<String, Object>();
		m.put("surveyId", surveyId);
		m.put("member", member);
		m.put("memberProfile", memberProfile);
		body = velocityService.formatByStringTemplate(survey.getTemplate(), m);
		return "survey";
	}

	public String saveSurvey() throws ParseException, IOException {
		String memberId = UserLoginData.getMemberId();
		if (memberService.isJoined(memberId, surveyId)) {
			return "finish";
		}
		if (StringUtils.isBlank(surveyId)) {
			return survey();
		}

		// 更新会员主档
		if (StringUtils.isNotBlank(sex) || StringUtils.isNotBlank(birth)) {
			member = memberService.get(memberId);
			if (StringUtils.isNotBlank(sex)) {
				member.setSex(sex);
			}
			if (StringUtils.isNotBlank(birth)) {
				DateFormat df = new SimpleDateFormat("yyyyMMdd");
				member.setBirthday(df.parse(birth));
			}
		}

		// 更新 or 储存 memberProfile
		memberProfile = memberService.getMemberProfile(memberId);
		if (null == memberProfile) {
			memberProfile = new MemberProfile();
			memberProfile.setMemberId(memberId);
		}
		if (StringUtils.isNotBlank(height)) {
			memberProfile.setHeight(height);
		}
		if (StringUtils.isNotBlank(weight)) {
			memberProfile.setWeight(weight);
		}
		if (StringUtils.isNotBlank(married)) {
			memberProfile.setMarried(married);
		}
		if (StringUtils.isNotBlank(edu)) {
			memberProfile.setEdu(edu);
		}
		if (StringUtils.isNotBlank(work)) {
			memberProfile.setWork(work);
		}
		if (StringUtils.isNotBlank(workType)) {
			memberProfile.setWorkType(workType);
		}
		if (StringUtils.isNotBlank(salary)) {
			memberProfile.setSalary(salary);
		}
		if (StringUtils.isNotBlank(shoesSize)) {
			memberProfile.setShoesSize(shoesSize);
		}

		// 储存问卷记录
		MemberMemo mm = new MemberMemo();
		mm.setMemberId(memberId);
		mm.setMemo1(surveyId);
		mm.setMemo2(new SimpleDateFormat("yyyy/MM/dd HH-mm-ss").format(new Date()));
		if (!memberService.saveSurveyData(member, mm, memberProfile)) {
			return survey();
		}

		// 儲存SurveyData
		surveyData = new SurveyData();
		surveyData.setSurveyId(surveyId);
		surveyData.setMemberId(memberId);
		surveyData.setCo1(co1);
		surveyData.setCo2(co2);
		surveyData.setCo3(co3);
		surveyData.setCo4(co4);
		surveyData.setCo5(co5);
		surveyData.setCo6(co6);
		surveyData.setCo7(co7);
		surveyData.setCo8(co8);
		surveyData.setCo9(co9);
		surveyData.setCo10(co10);
		surveyData.setCo11(co11);
		surveyData.setCo12(co12);
		surveyData.setCo13(co13);
		surveyData.setCo14(co14);
		surveyData.setCo15(co15);
		surveyData.setCo16(co16);
		surveyData.setCo17(co17);
		surveyData.setCo18(co18);
		surveyData.setCo19(co19);
		surveyData.setCo20(co20);
		surveyData.setCo21(co21);
		surveyData.setCo22(co22);
		surveyData.setCo23(co23);
		surveyData.setCo24(co24);
		surveyData.setCo25(co25);
		surveyData.setCo26(co26);
		surveyData.setCo27(co27);
		surveyData.setCo28(co28);
		surveyData.setCo29(co29);
		surveyData.setCo30(co30);
		surveyService.saveOrUpdateSurveyData(surveyData);

		return "finish";
	}

	public SurveyService getSurveyService() {
		return surveyService;
	}

	public void setSurveyService(SurveyService surveyService) {
		this.surveyService = surveyService;
	}

	public String getSurveyId() {
		return surveyId;
	}

	public void setSurveyId(String surveyId) {
		this.surveyId = surveyId;
	}

	public MemberService getMemberService() {
		return memberService;
	}

	public void setMemberService(MemberService memberService) {
		this.memberService = memberService;
	}

	public MemberProfile getMemberProfile() {
		return memberProfile;
	}

	public void setMemberProfile(MemberProfile memberProfile) {
		this.memberProfile = memberProfile;
	}

	public String getSex() {
		return sex;
	}

	public void setSex(String sex) {
		this.sex = sex;
	}

	public String getBirth() {
		return birth;
	}

	public void setBirth(String birth) {
		this.birth = birth;
	}

	public String getMarried() {
		return married;
	}

	public void setMarried(String married) {
		this.married = married;
	}

	public String getEdu() {
		return edu;
	}

	public void setEdu(String edu) {
		this.edu = edu;
	}

	public String getHeight() {
		return height;
	}

	public void setHeight(String height) {
		this.height = height;
	}

	public String getWeight() {
		return weight;
	}

	public void setWeight(String weight) {
		this.weight = weight;
	}

	public String getWork() {
		return work;
	}

	public void setWork(String work) {
		this.work = work;
	}

	public String getWorkType() {
		return workType;
	}

	public void setWorkType(String workType) {
		this.workType = workType;
	}

	public String getShoesSize() {
		return shoesSize;
	}

	public void setShoesSize(String shoesSize) {
		this.shoesSize = shoesSize;
	}

	public String getSalary() {
		return salary;
	}

	public void setSalary(String salary) {
		this.salary = salary;
	}

	public String getCo1() {
		return co1;
	}

	public void setCo1(String co1) {
		this.co1 = co1;
	}

	public String getCo2() {
		return co2;
	}

	public void setCo2(String co2) {
		this.co2 = co2;
	}

	public String getCo3() {
		return co3;
	}

	public void setCo3(String co3) {
		this.co3 = co3;
	}

	public String getCo4() {
		return co4;
	}

	public void setCo4(String co4) {
		this.co4 = co4;
	}

	public String getCo5() {
		return co5;
	}

	public void setCo5(String co5) {
		this.co5 = co5;
	}

	public String getCo6() {
		return co6;
	}

	public void setCo6(String co6) {
		this.co6 = co6;
	}

	public String getCo7() {
		return co7;
	}

	public void setCo7(String co7) {
		this.co7 = co7;
	}

	public String getCo8() {
		return co8;
	}

	public void setCo8(String co8) {
		this.co8 = co8;
	}

	public String getCo9() {
		return co9;
	}

	public void setCo9(String co9) {
		this.co9 = co9;
	}

	public String getCo10() {
		return co10;
	}

	public void setCo10(String co10) {
		this.co10 = co10;
	}

	public String getCo11() {
		return co11;
	}

	public void setCo11(String co11) {
		this.co11 = co11;
	}

	public String getCo12() {
		return co12;
	}

	public void setCo12(String co12) {
		this.co12 = co12;
	}

	public String getCo13() {
		return co13;
	}

	public void setCo13(String co13) {
		this.co13 = co13;
	}

	public String getCo14() {
		return co14;
	}

	public void setCo14(String co14) {
		this.co14 = co14;
	}

	public String getCo15() {
		return co15;
	}

	public void setCo15(String co15) {
		this.co15 = co15;
	}

	public String getCo16() {
		return co16;
	}

	public void setCo16(String co16) {
		this.co16 = co16;
	}

	public String getCo17() {
		return co17;
	}

	public void setCo17(String co17) {
		this.co17 = co17;
	}

	public String getCo18() {
		return co18;
	}

	public void setCo18(String co18) {
		this.co18 = co18;
	}

	public String getCo19() {
		return co19;
	}

	public void setCo19(String co19) {
		this.co19 = co19;
	}

	public String getCo20() {
		return co20;
	}

	public void setCo20(String co20) {
		this.co20 = co20;
	}

	public String getCo21() {
		return co21;
	}

	public void setCo21(String co21) {
		this.co21 = co21;
	}

	public String getCo22() {
		return co22;
	}

	public void setCo22(String co22) {
		this.co22 = co22;
	}

	public String getCo23() {
		return co23;
	}

	public void setCo23(String co23) {
		this.co23 = co23;
	}

	public String getCo24() {
		return co24;
	}

	public void setCo24(String co24) {
		this.co24 = co24;
	}

	public String getCo25() {
		return co25;
	}

	public void setCo25(String co25) {
		this.co25 = co25;
	}

	public String getCo26() {
		return co26;
	}

	public void setCo26(String co26) {
		this.co26 = co26;
	}

	public String getCo27() {
		return co27;
	}

	public void setCo27(String co27) {
		this.co27 = co27;
	}

	public String getCo28() {
		return co28;
	}

	public void setCo28(String co28) {
		this.co28 = co28;
	}

	public String getCo29() {
		return co29;
	}

	public void setCo29(String co29) {
		this.co29 = co29;
	}

	public String getCo30() {
		return co30;
	}

	public void setCo30(String co30) {
		this.co30 = co30;
	}

	public Member getMember() {
		return member;
	}

	public void setMember(Member member) {
		this.member = member;
	}

	public SurveyData getSurveyData() {
		return surveyData;
	}

	public void setSurveyData(SurveyData surveyData) {
		this.surveyData = surveyData;
	}

	public String getResult() {
		return result;
	}

	public void setResult(String result) {
		this.result = result;
	}

	public TemplateService getVelocityService() {
		return velocityService;
	}

	public void setVelocityService(TemplateService velocityService) {
		this.velocityService = velocityService;
	}

	public String getBody() {
		return body;
	}

	public void setBody(String body) {
		this.body = body;
	}

}